package com.atguigu.eduservice.controller;


import com.atguigu.commentutils.R;
import com.atguigu.eduservice.client.VodClient;
import com.atguigu.eduservice.entity.EduVideo;
import com.atguigu.eduservice.service.EduVideoService;
import com.atguigu.servicebase.config.exceptionhandler.GuliException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 课程视频 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2022-10-11
 */
@Api(description = "小节管理")
@RestController
@RequestMapping("/eduservice/edvideo")
public class EduVideoController {

    //注入service
    @Autowired
    private EduVideoService videoService;
    @Autowired
    private VodClient vodClient;

    //添加小节
    @ApiOperation(value = "添加小节")
    @PostMapping("addVideo")
    public R addVideo(@RequestBody EduVideo video){
        boolean save = videoService.save(video);
        if (save) return R.ok();
        else return R.error();
    }

    //删除小节
    @ApiOperation(value = "删除小节")
    @DeleteMapping("{videoId}")
    public R deleteVideo(@PathVariable String videoId){
        //根据小节ID获取视频ID
        EduVideo video = videoService.getById(videoId);
        String videoSourceId = video.getVideoSourceId();
        if (!StringUtils.isEmpty(videoSourceId)){
            //删除视频
            R result = vodClient.removeVideo(videoSourceId);
            if (result.getCode() == 20001){
                throw new GuliException(20001,"删除视频失败，熔断器----");
            }
        }

        boolean remove = videoService.removeById(videoId);
        if (remove) return R.ok();
        else return R.error();
    }
}

